<template>
  <el-dialog
    :title="!dataForm.merchantId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商家账号" prop="merchantName">
      <el-input v-model="dataForm.merchantName" placeholder="商家账号"></el-input>
    </el-form-item>
    <el-form-item label="商家密码" prop="merchantPwd">
      <el-input v-model="dataForm.merchantPwd" placeholder="商家密码"></el-input>
    </el-form-item>
    <el-form-item label="商家房源" prop="merchantHouse">
      <el-input v-model="dataForm.merchantHouse" placeholder="商家房源"></el-input>
    </el-form-item>
    <el-form-item label="商家电话" prop="merchantPhone">
      <el-input v-model="dataForm.merchantPhone" placeholder="商家电话"></el-input>
    </el-form-item>
    <el-form-item label="新房Id" prop="newId">
      <el-input v-model="dataForm.newId" placeholder="新房Id"></el-input>
    </el-form-item>
    <el-form-item label="二手房Id" prop="oldId">
      <el-input v-model="dataForm.oldId" placeholder="二手房Id"></el-input>
    </el-form-item>
    <el-form-item label="租房Id" prop="rentId">
      <el-input v-model="dataForm.rentId" placeholder="租房Id"></el-input>
    </el-form-item>
    <el-form-item label="地区Id" prop="mapId">
      <el-input v-model="dataForm.mapId" placeholder="地区Id"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          merchantId: 0,
          merchantName: '',
          merchantPwd: '',
          merchantHouse: '',
          merchantPhone: '',
          newId: '',
          oldId: '',
          rentId: '',
          mapId: '',
          userId: ''
        },
        dataRule: {
          merchantName: [
            { required: true, message: '商家账号不能为空', trigger: 'blur' }
          ],
          merchantPwd: [
            { required: true, message: '商家密码不能为空', trigger: 'blur' }
          ],
          merchantHouse: [
            { required: true, message: '商家房源不能为空', trigger: 'blur' }
          ],
          merchantPhone: [
            { required: true, message: '商家电话不能为空', trigger: 'blur' }
          ],
          newId: [
            { required: true, message: '新房Id不能为空', trigger: 'blur' }
          ],
          oldId: [
            { required: true, message: '二手房Id不能为空', trigger: 'blur' }
          ],
          rentId: [
            { required: true, message: '租房Id不能为空', trigger: 'blur' }
          ],
          mapId: [
            { required: true, message: '地区Id不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.merchantId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.merchantId) {
            this.$http({
              url: this.$http.adornUrl(`/house/merchant/info/${this.dataForm.merchantId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.merchantName = data.merchant.merchantName
                this.dataForm.merchantPwd = data.merchant.merchantPwd
                this.dataForm.merchantHouse = data.merchant.merchantHouse
                this.dataForm.merchantPhone = data.merchant.merchantPhone
                this.dataForm.newId = data.merchant.newId
                this.dataForm.oldId = data.merchant.oldId
                this.dataForm.rentId = data.merchant.rentId
                this.dataForm.mapId = data.merchant.mapId
                this.dataForm.userId = data.merchant.userId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/merchant/${!this.dataForm.merchantId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'merchantId': this.dataForm.merchantId || undefined,
                'merchantName': this.dataForm.merchantName,
                'merchantPwd': this.dataForm.merchantPwd,
                'merchantHouse': this.dataForm.merchantHouse,
                'merchantPhone': this.dataForm.merchantPhone,
                'newId': this.dataForm.newId,
                'oldId': this.dataForm.oldId,
                'rentId': this.dataForm.rentId,
                'mapId': this.dataForm.mapId,
                'userId': this.dataForm.userId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
